
var user;

//获取课程列表
CourseList = function () {

    $('#studentTable').bootstrapTable('destroy');
    $('#studentTable').bootstrapTable({
        //样式调整
        striped: true,
        formatter: true,

        //后台获取数据
        url: 'getAllCourseByStudent',
        //id
        idField: 'courseId',
        columns: [{
            field: 'courseId',
            title: 'courseId',
            visible: false
        },{
            field: 'courseName',
            title: '课程名',
            width: 100
        }, {
            field: 'userId',
            title: 'userId',
            visible: false
        },{
            field: 'startTime',
            title: 'startTime',
        },{
            field: 'endTime',
            title: 'endTime',
        },{
            field: 'operation',
            title: '操作',
            width: 200,
            formatter: function (value, row, index) {
                return "<button class='btn btn-info btn-sm' id='jumpAssByCourseId'"
                    + "onclick='AssignmentList(" + row.courseId + ")'>进入</button>";
            }
        }]
    });
}

//Assignment的相关函数
//获取assignment集合table
function AssignmentList(courseId){

    $('#studentTable').bootstrapTable('destroy');
    $('#studentTable').bootstrapTable({
        //样式调整
        striped: true,
        formatter: true,
        //后台获取数据
        url: 'getAllAssignment',
        method: "post",
        contentType:"application/x-www-form-urlencoded; charset=UTF-8",
        queryParams: getParams(courseId),
        dataType: "json",
        //idc
        idField: 'assignmentId',
        columns: [{
            field: 'assignmentd',
            title: 'assignmentId',
            visible: false
        },{
            field: 'courseId',
            title: 'courseId',
            width: 100,
            visible: false
        },{
            field: 'assignmentName',
            title: '课程名',
            width: 100
        }, {
            field: 'description',
            title: '描述',
        },{
            field: 'startTime',
            title: 'startTime',
        },{
            field: 'endTime',
            title: 'endTime',
        },{
            field: 'operation',
            title: '操作',
            width: 200,
            formatter: function (value, row, index) {
                return "<button class='btn btn-info btn-sm' " +
                    "id='jumpActByAssignmentId' onclick=" +
                    "'ActivityList(" + row.assignmentId + ")'>进入</button>";
            }
        }]
    });
}
//传递参数
getParams = function (courseId) {
    var param = {
        courseId: courseId
    };
    return param;
};

/* 活动 相关 的代码*/

// 获取 activity 列表
function ActivityList(assignmentId,courseId){

    $('#studentTable').bootstrapTable('destroy');
    $('#studentTable').bootstrapTable({
        //样式调整
        striped: true,
        formatter: true,
        //后台获取数据
        url: 'getAllActivity',
        method: "post",
        contentType:"application/x-www-form-urlencoded; charset=UTF-8",
        queryParams: getActivityParams(assignmentId),
        dataType: "json",
        //id
        idField: 'activityId',
        columns: [{
            field: 'activityId',
            title: 'activityId',
            visible: false
        },{
            field: 'assignmentId',
            title: 'assignmentId',
            width: 100,
            visible: false
        },{
            field: 'activityName',
            title: '活动名',
            width: 100
        }, {
            field: 'type',
            title: '类型',
        },{
            field: 'description',
            title: '描述',
        },{
            field: 'startTime',
            title: 'startTime',
        },{
            field: 'endTime',
            title: 'endTime',
        },{
            field: 'operation',
            title: '操作',
            width: 200,
            formatter: function (value, row, index) {
                return "<button class='btn btn-info btn-sm' " +
                    "id='jumpActByAssignmentId' " +
                    "onclick=jumpPage('" + row.type + "'," + row.activityId + ")>" +
                    "进入</button>";
            }
        }],
    });
}
//传递参数
getActivityParams = function (assignmentId) {
    var param = {
        assignmentId: assignmentId
    };
    return param;
}

//根据活动type跳转页面
jumpPage = function (type,activityId) {
    console.log("点击了跳转按钮: "+type);
    if(type=="讨论室"){
        window.location.href='./chatting.html?id='+user.id;
    }
    if(type=="对话"){
        setCookie("activityId",activityId);
        window.location.href='./dialog.html';
    }
}

var jsonUser = getCookie("user");
console.log(jsonUser);
var user = JSON.parse(jsonUser);
if(user!=null){
    $("#userName").text(user.realname+"同学");
}
if(user==null){
    window.location.href='./index.html';
}
CourseList();